define(function (require, exports, module) {
    'use strict';

    function ctrl($scope,$state,$stateParams,InfoService,HistoryNavChartService,CommonService,$ionicScrollDelegate,
                  $ionicHistory,$ionicLoading) {

        var par = $scope.param = new Array();
        par.prod_code = $stateParams['prod_code'];
        par.is_index = $stateParams['is_index'];
        if(par.is_index == '1'){
            //画沪深300基金净值走势图
            par.historyNavChartConfig = HistoryNavChartService.historyNavCSI300Chart();
        }else{
            //画累计净值走势图
            par.historyNavChartConfig = HistoryNavChartService.historyNavChart(null);
        }

        $scope.$on('$ionicView.beforeEnter', function() {
            par.duration_detail = false; //是否显示期限详情
            init();
        });

        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        function init(){
            if(!$scope.fund){
                $ionicLoading.show();
            }
            par.show_chart = true;
            //获取cms基金详情
            return InfoService.getFundGeneral(par.prod_code).then(function(data){
                if(data){
                    $scope.fund = data;
                    if($scope.fund.prod_min_subscribe){
                        $scope.fund.prod_min_subscribe = CommonService.getMinAmount($scope.fund);
                    }
                    par.title = $scope.fund.prod_name;
                    if("0,2,3".indexOf($scope.fund.prod_profit_mode) > -1){
                        $state.go("tab.product-item", {prod_code : $scope.fund.prod_code});
                    }
                    //画图
                    drawChart($scope.fund.prod_id);
                }
                $ionicLoading.hide();
            });
        }
        $scope.onBackKeyDown = function(){
            if($scope.param.duration_detail) {
                $scope.param.duration_detail = false;
                par.title = $scope.fund.prod_name;
                $ionicScrollDelegate.scrollTo(0,par.scrollTop,false);
            } else {
                if($ionicHistory.backView() == null) {
                    $state.go("tab.index");
                } else {
                    $ionicHistory.goBack();
                }
            }
        };

        $scope.showProdDurationDetail = function(){
            if(!CommonService.isStrEmpty($scope.fund.prod_duration_desc)){
                if($scope.fund.prod_status == 4){
                    par.title = "认购期说明";
                }else if($scope.fund.prod_status == 5){
                    par.title = "封闭期说明";
                }else{
                    par.title = "期限说明";
                }
                par.duration_detail = true;
                par.scrollTop=$ionicScrollDelegate.getScrollPosition().top;
                $ionicScrollDelegate.scrollTop(false);
            }
        };

        function drawChart(code){
            if(par.is_index == '1'){
                InfoService.getComparisonData(code,60).then(function(data){
                    if(data && data.history_nav &&  data.history_nav.length >0 ){
                        //画沪深300基金净值走势图
                        par.historyNavChartConfig = HistoryNavChartService.historyNavCSI300Chart(data.history_nav,data.CSI300);
                    } else {
                        par.show_chart = false;
                    }
                });
            }else{
                InfoService.getHistoryNav(code,60).then(function(data){
                    if(data && data.length > 0){
                        //画累计净值走势图
                       par.historyNavChartConfig = HistoryNavChartService.historyNavChart(data);
                    } else {
                        par.show_chart = false;
                    }
                });
            }
        };
    }

    ctrl.$inject = ['$scope','$state','$stateParams','InfoService','HistoryNavChartService','CommonService',
        '$ionicScrollDelegate','$ionicHistory','$ionicLoading'];
    return ctrl;
});